package com.project.service.impl;

import com.project.dto.merchantToRider.OrderDto;
import com.project.entity.SysComment;
import com.project.entity.SysOrder;
import com.project.entity.SysOrderDetail;
import com.project.entity.SysStore;
import com.project.mapper.SysStoreMapper;
import com.project.mapper.WeProvideTheInterfaceMapper;
import com.project.service.IWeProvideTheInterfaceService;
import com.project.util.codingUtils.RedisCacheUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author(王思翰)
 * @Description(类的描述) 我们提供的业务层实现类
 * @Date(创建时间) 2021/10/8 10:13
 */
@Service
@Transactional
public class WeProvideTheInterfaceServiceImpl implements IWeProvideTheInterfaceService {
    /**
     * redis工具类
     */
    static RedisCacheUtil redisCacheUtil = new RedisCacheUtil();
    @Autowired
    WeProvideTheInterfaceMapper weProvideTheInterfaceMapper;
    @Autowired
    SysStoreMapper StoreMapper;
    @Autowired
    //rabbitMq模板
    RabbitTemplate rabbitTemplate;


    /**
     * 向后台系统提供所有商家信息
     * 查询所有商家信息
     *
     * @return 商家集合
     */
    @Override
    public List<SysStore> findByAll() {
        return weProvideTheInterfaceMapper.findByAll();
    }

    /**
     * 提供填写评论和填写星级接口给用户端口
     * 根据用户提供的用户编号和商品id添加评论
     *
     * @param userCode     用户编号
     * @param storeId      商铺id
     * @param storeStar    评论星级
     * @param storeComment 评论内容
     * @return
     */
    @Override
    public boolean addCommentByUser(String userCode, Integer storeId, Integer storeStar, String storeComment) {
        SysComment comment = new SysComment();
        comment.getUser().setUserCode(userCode);
        comment.getStore().setPkStoreid(storeId);
        comment.setCClaims(storeStar);
        comment.setCInfo(storeComment);

        weProvideTheInterfaceMapper.addCommentByUser(comment);

        return false;
    }

    /**
     * 提供给骑手所有可接单的订单
     * 通过查询可接单的订单（状态为1的订单）
     *
     * @return 订单集合
     */
    @Override
    public List<SysOrder> findByOrder() {
        List<SysOrder> list = weProvideTheInterfaceMapper.findByOrder();

        for (SysOrder order : list) {
            OrderDto orderDto = new OrderDto();
            List<SysOrderDetail> detailEntries = order.getOrderDetails();

            orderDto.setOrderId(order.getPkOrderid());
            orderDto.setCode(order.getONumber());
            orderDto.setPrice(order.getOMoney()+"");
            orderDto.setStartTime(order.getOTime());
            //查询该订单所属店铺id查询店铺信息并将有用的字段提取并赋值
            SysStore store = StoreMapper.findById(order.getStore().getPkStoreid());
            orderDto.setArea(store.getBusinessInfo().getBOpenArea());
            orderDto.setMerchantTel(store.getBusinessInfo().getPrincipal().getPPhone());
            orderDto.setMerchantAddress(store.getBusinessInfo().getBAddress());


            rabbitTemplate.convertAndSend("directExchange", "Merchant.Queue", orderDto);
        }

        return list;
    }
}
